﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using ClassLibrary.Utilities;

public partial class usercontrol_ucRegister : System.Web.UI.UserControl
{
    TblUserAccountDAO userDAO = new TblUserAccountDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public bool UserAccount_CheckExit()
    {
        bool flag = false;
        try
        {
              flag = userDAO.CheckExits(txtEmail.Text, 1);
        }
        catch(Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Error.aspx", true);
        }
        return flag;
    }

    protected void lnkRefreshCaptcha_Click(object sender, EventArgs e)
    {
        txtCaptcha.Text = string.Empty;
    }


    protected void btdangky_Click(object sender, EventArgs e)
    {
        bool bFlag = false;

        lblMessage.Text = String.Empty;
        msCaptcha.ValidateCaptcha(txtCaptcha.Text);
        if (!msCaptcha.UserValidated)
        {
            lblMessage.Text = "Mã xác nhận không đúng";
            txtCaptcha.Text = String.Empty;
            lblMessage.ForeColor = System.Drawing.Color.Red;
            return;
        }
        if (UserAccount_CheckExit())
        {
            lblMessage.Text = "Địa chỉ Email này đã tồn tại.";
            txtCaptcha.Text = String.Empty;
            return;
        }

        try
        {
            // EmailService emailSrv = new EmailService();

            TblUserAccount user = new TblUserAccount();


            user.FirstName = txtFName.Text;
            user.LastName = txtSName.Text;
            user.Email = txtEmail.Text;

            user.Password = Encryption.EncryptToString(txtPassword.Text);
            user.AccountStatus = 1;

            user.Newsletter = chkNewsletter.Checked;

            userDAO.Insert_Update(user, 1);
            //Send mail     
            //  user = userDAO.GetAll(user.Email);
            //  string actLink = "http://" + Request.Url.Host + "//Activate.aspx?aid=" + Encryption.EncryptToString(user.AccountID.ToString()) + "&ac=" + Encryption.EncryptToString(user.ActivatedCode);

            //   emailSrv.SendWelcomeMembershipEmail(user, Server.MapPath(Global.MEMBERSHIP_WELCOME), actLink);

            bFlag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
          
        }
        if (bFlag)
        {
            //lblMessage.Text = "Your account has been registered successfully.<br /><br />Please check your email <b><u>" + txtEmail.Text.Trim() + "</u></b> to activate before logging the website.";
            lblMessage.Text = " Bạn đã đăng ký thành công với địa chỉ email <b><u>" + txtEmail.Text.Trim() + "</u></b>. Hãy đăng nhập hệ thống để mua hàng ";
            lblMessage.ForeColor = System.Drawing.Color.Blue;
            pnlRegister.Visible = false;
        }
    }
}
